package com.blb.ssh.action;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.blb.ssh.entity.Menu;
import com.blb.ssh.entity.Role;
import com.blb.ssh.entity.User;
import com.blb.ssh.exception.BusinessException;
import com.blb.ssh.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
	private User user;
	private UserService userService;
	
	private int page;
	private int rows;
	private String ids;
	private int menui;
	private List<Role> resultList;
	private List<Menu> menuList;
	private Map<String,Object> resultMap =new HashMap<String,Object>();

	//登录
	public String login(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=ServletActionContext.getRequest().getSession();
		String RememberMe = request.getParameter("RememberMe");
		try{
			User u=userService.login(user.getUsername(),user.getPassword());
			session.setAttribute("Roleid", u.getRoleid());
			if("on".equals(RememberMe)){
				session.setAttribute("u", u);
			}
		}catch(BusinessException e){
			e.printStackTrace();
			request.setAttribute("msg", e.getMessage());
			return "error";
		}
		return "success";
	}
	
	
	//查询
	public String selectUserPageData(){
		Long userCount=userService.getUserCount();
		List<User> userList=userService.getUserListByMap(page,rows);
		
		resultMap.put("total", userCount);
		resultMap.put("rows", userList);
		
		return "data";
	}
	
	//用户删除
	public String batchDeleteUser(){
		resultMap.put("isok", userService.batchDeleteUser(ids));
		return "data";
	}
	
	//查找role信息
	public String selectAllRoleList(){
		resultList=userService.getAllRoleList();
		return "list";
	}
	//新增用户
	public String addUser(){
		resultMap.put("isok", userService.addUser(user));
		return "data";
	}

	//修改用户
	public String editUser(){
		resultMap.put("isok", userService.editUser(user));
		return "data";
	}
	
	public String buttonControl(){
		menuList=userService.buttonControl(menui);
		return "list2";
	}
	
	
	
	
	
	
	public int getMenui() {
		return menui;
	}


	public void setMenui(int menui) {
		this.menui = menui;
	}


	public List<Menu> getMenuList() {
		return menuList;
	}


	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}


	public List<Role> getResultList() {
		return resultList;
	}

	public void setResultList(List<Role> resultList) {
		this.resultList = resultList;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}


	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
}
